package com.solution.admin.report.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.report.entity.CustomerOrderDailyDataEntity;
import com.solution.admin.report.service.CustomerOrderDailyDataService;
import com.solution.common.utils.DateUtils;
import com.solution.common.utils.string.StringUtil;

/**
 * 客户增长分析报表控制层
 * 
 * @author: solin
 * @date: 2018年1月3日 下午2:43:33
 */
@RequestMapping("/report")
@Controller
public class CustomerGrowthAnalysisController {

	@Autowired
	CustomerOrderDailyDataService dailyDataService;

	/**
	 * 跳转到客户增长分析页面
	 * 
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2018年1月3日 下午2:51:54
	 */
	@RequestMapping("/toCustomerGrowthAnalysisPage")
	public String toCustomerGrowthAnalysisPage(HttpServletRequest request, Model model) {
		return "report/customerGrowthAnalysis";
	}

	/**
	 * 获取客户增长数据
	 * 
	 * @param request
	 * @param begin
	 * @param end
	 * @return List<Map<String,Object>>
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2018年1月6日 下午5:56:48
	 */
	@RequestMapping("/getCustomerGrowthData")
	@ResponseBody
	public List<CustomerOrderDailyDataEntity> getCustomerGrowthData(HttpServletRequest request, String begin, String end) {
		List<CustomerOrderDailyDataEntity> dataList = new ArrayList<CustomerOrderDailyDataEntity>();
		try {
			if (StringUtil.isEmpty(begin)) {
				begin = DateUtils.getDate();
			}
			if (StringUtil.isEmpty(end)) {
				end = DateUtils.getDate();
			}
			ArrayList<String> dateList = getBetweenDate(begin, end);
			CustomerOrderDailyDataEntity dailyData = null;
			for (int i = 0; i < dateList.size(); i++) {
				if(i != dateList.size()-1){
					dailyData = dailyDataService.getDailyDataByDate(dateList.get(i));
					dataList.add(dailyData);
				}
			}
		} catch (Exception ec) {
			ec.printStackTrace();
		}
		return dataList;
	}

	/**
	 * 两个日期之间的所有日期
	 * 
	 * @param begin
	 * @param end
	 * @return ArrayList<String>
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2018年1月6日 下午5:53:18
	 */
	private ArrayList<String> getBetweenDate(String begin, String end) {
		ArrayList<String> result = new ArrayList<String>();
		try {
			SimpleDateFormat paramFormat = new SimpleDateFormat("yyyy-MM-dd");// 格式化为年月
			Calendar min = Calendar.getInstance();
			Calendar max = Calendar.getInstance();
			min.setTime(paramFormat.parse(begin));
			min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), min.get(Calendar.DATE));
			max.setTime(paramFormat.parse(end));
			max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), max.get(Calendar.DATE) + 1);
			Calendar curr = min;
			while (curr.before(max)) {
				result.add(paramFormat.format(curr.getTime()));
				curr.add(Calendar.DATE, 1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
